package cn.enilu.flash.api.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;

public class AliPayment {

    public String createPayForm(String subject,String tradeno,String price,String notifyUrl,String returnUrl) {

        try {
            AlipayClient alipayClient = new DefaultAlipayClient(getClientParams());
            AlipayTradeWapPayRequest request = getRequest(subject, tradeno, price, notifyUrl,returnUrl);
            AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
            if(response.isSuccess()){
                return response.getBody();
            }
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    private CertAlipayRequest getClientParams() {
        CertAlipayRequest certParams = new CertAlipayRequest();
        certParams.setServerUrl("https://openapi.alipay.com/gateway.do");
        //请更换为您的AppId
        certParams.setAppId("2021003115610434");
        //请更换为您的PKCS8格式的应用私钥
        certParams.setPrivateKey("MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCKyZhSqc4LeuBzNgCbsOrfs/w4VPmigW0XVaF0tcudsAo97DJoXZtd7O378QY8jlwvOFs6//9Cl/t0vu/A9Fmjkbm8fwx0osIleCsK025xTREDHOzSOWoynTsQTT5bsx7eNDYP17iMgeRW+sfsfoFCEqOqWt7wsOqCwsfEcGAPPNY8Flw3BULToQTcWlS6eXEq7iHdLDOjcYwxPYfBhtB5eO7zomdoTnww9Qax0qcRGgIMLJcgwkb/a3z3SyzLQmSVZPCq8N/7ZFy8pEvma+PdJOlNbwcnJBehjT1QOxl//9P73JmJicUB7niqYK73G6I+hkOwDSQMNpq0MKfxL0AZAgMBAAECggEBAIbxFhqhoYYF9j/u7M50SRoy3PREouf/YXuDtdVNgWmTpZGpmyVghlTfrlD9e2vbmwQKjqGiEKXGMibEE0cQDiv835PID+tD6PMXk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OjoIpsY+8uxwOEFK7xvZG4shtf1ByPTIsiAzqgY2RhK7+EzcH4PwhQg==");
        //请更换为您使用的字符集编码，推荐采用utf-8
        certParams.setCharset("utf-8");
        certParams.setFormat("json");
        certParams.setSignType("RSA2");
	String path = "/www/server/tomcat/diandiancuc/flash-waimai-master/flash-waimai-api/src/main/crt";
        //请更换为您的应用公钥证书文件路径
        certParams.setCertPath(path + "/appCertPublicKey_2021003115610434.crt");
        //请更换您的支付宝公钥证书文件路径
        certParams.setAlipayPublicCertPath(path+"/alipayCertPublicKey_RSA2.crt");
        //更换为支付宝根证书文件路径
        certParams.setRootCertPath(path+"/alipayRootCert.crt");
        
        return certParams;
    }

    private AlipayTradeWapPayRequest getRequest(String subject,String tradeno,String price,String notifyUrl,String returnUrl) {
        // 初始化Request，并填充Model属性。实际调用时请替换为您想要使用的API对应的Request对象。
        //AlipayOpenOperationOpenbizmockBizQueryRequest request = new AlipayOpenOperationOpenbizmockBizQueryRequest();
        //AlipayOpenOperationOpenbizmockBizQueryModel model = new AlipayOpenOperationOpenbizmockBizQueryModel();
        
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();


        model.setSubject(subject);

        model.setOutTradeNo(tradeno);

        model.setTotalAmount(price);

        model.setProductCode("23");

        model.setPassbackParams(tradeno);


        request.setBizModel(model);

        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        

        
        return request;
    }

    
}
